<template>
  <div class="dialog-form">
    <el-form ref="departmentRef" :model="form" label-width="100px">
      <el-form-item label="所属公司" prop="companyId">{{ form.companyName }}</el-form-item>
      <el-form-item label="部门名称" prop="name">{{ form.name }}</el-form-item>
      <el-form-item label="部门负责人" prop="debtor">{{ form.debtor }}</el-form-item>
      <el-form-item label="显示排序" prop="sorts">{{ form.sorts }}</el-form-item>
      <el-form-item label="部门状态" prop="state">
        <dict-tag :options="sys_company_status" :value="form.state" />
      </el-form-item>
    </el-form>
    <div class="dialog-footer">
      <el-button type="primary" @click="onCancel">关闭</el-button>
    </div>
  </div>
</template>

<script setup>
import { listDepartment, getDepartment, delDepartment, addDepartment, updateDepartment } from "@/api/company/department"

const { proxy } = getCurrentInstance()
const { sys_company_status }  = proxy.useDict('sys_company_status')

const emits = defineEmits(['cancel', 'confirm'])
const props = defineProps(['companyList'])

const form = ref({})

const pageInit = (id) => {
  getDepartment(id).then(response => {
    form.value = response.data
    form.value.state = form.value.state.toString()
    for (let i = 0; i < props.companyList.length; i++) {
      let item = props.companyList[i]
      if (item.id == form.value.companyId) {
        form.value.companyName = item.name
        break
      } else if (item.children && item.children.length) {
        for (let j = 0; j < item.children.length; j++) {
          let obj = item.children[j]
          if (obj.id == form.value.companyId) {
            form.value.companyName = item.name + ' > ' + obj.name
            break
          }
        }
      }
    }
  })
}

const onCancel = () => {
  emits('cancel')
}

defineExpose({
  pageInit
})

</script>

<style lang="scss" scoped>

</style>